/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package fpsman;

import com.jme3.niftygui.NiftyJmeDisplay;
import de.lessvoid.nifty.Nifty;
import de.lessvoid.nifty.controls.Label;
import de.lessvoid.nifty.controls.TextField;
import de.lessvoid.nifty.elements.Element;
import de.lessvoid.nifty.screen.Screen;
import de.lessvoid.nifty.screen.ScreenController;
import java.util.Iterator;
import java.util.Map;
import java.util.TreeMap;

/**
 *
 * @author David
 */
public class niftyController implements ScreenController {

    private Nifty nifty;
    private Game owner;
    private Screen screen;
    

    public niftyController(Game owner) {
        this.owner = owner;
        NiftyJmeDisplay niftyDisplay = new NiftyJmeDisplay(owner.getAssetManager(),
                owner.getInputManager(),
                owner.getAudioRenderer(),
                owner.getGuiViewPort());

        nifty = niftyDisplay.getNifty();

        // attach the nifty display to the gui view port as a processor
        owner.getGuiViewPort().addProcessor(niftyDisplay);

        nifty.fromXml("Interface/screens.xml", "start2", this);
        // disable the fly cam
        owner.getFlyByCamera().setEnabled(false);
        owner.getFlyByCamera().setDragToRotate(true);

        owner.getInputManager().setCursorVisible(true);
    }

    @Override
    public void bind(Nifty nifty, Screen screen) {
        this.screen = screen;
    }

    @Override
    public void onStartScreen() {
    }

    @Override
    public void onEndScreen() {
    }

    public void salvaPlacar() {
        TextField text = screen.findNiftyControl("nome", TextField.class);
        Element e = screen.findElementByName("nome");
        e.setVisible(false);
        
        Element e2 = screen.findElementByName("buttonOk");
        e2.setVisible(false);
        String nome = text.getText();
        owner.salvaPlacar(nome);
        getPlacar();
        e2 = screen.findElementByName("btnRest");
        e2.setVisible(true);
        e2 = screen.findElementByName("btnQuit");
        e2.setVisible(true);
        
    }
    
    private void getPlacar()
    {
        TreeMap<Integer,String> placar = owner.getPlacar();
        Iterator<Map.Entry<Integer,String>> it = placar.entrySet().iterator();
        for(int i = 1; i <= 5; i++)
        {
            if(it.hasNext())
            {
                Map.Entry<Integer,String> e = it.next();
                Label label = screen.findNiftyControl("label"+i, Label.class);
                label.setText(e.getKey() + "\t" + e.getValue());
            }
            else
                break;
        }
    }

    public void quitGame() {
        owner.stop();
    }

    public void startGame(String nextScreen) {        
        owner.getFlyByCamera().setDragToRotate(false);

        owner.getInputManager().setCursorVisible(false);
        owner.setPlaying(true, true);

        if (owner.isPaused()) {
            owner.resumeGame();
        } else {
            owner.startGame();
        }
        nifty.gotoScreen(nextScreen);  // switch to another screen
    }

    public void pauseGame() {
        owner.getFlyByCamera().setDragToRotate(true);
        owner.getInputManager().setCursorVisible(true);
        nifty.gotoScreen("pausedGame");
    }

    public void restartGame(String nextScreen) {
        owner.getFlyByCamera().setDragToRotate(false);

        owner.getInputManager().setCursorVisible(false);
        owner.setPlaying(true, true);

        owner.restartGame();
        nifty.gotoScreen(nextScreen);
    }
    
    public void restartGame2(String nextScreen){
        Element e = screen.findElementByName("nome");
        e.setVisible(true);
        TextField text = screen.findNiftyControl("nome", TextField.class);
        text.setText("");
        Element e2 = screen.findElementByName("buttonOk");
        e2.setVisible(true);
        e2 = screen.findElementByName("btnRest");
        e2.setVisible(false);
        e2 = screen.findElementByName("btnQuit");
        e2.setVisible(false);
        restartGame(nextScreen);
    }

    public void gameOver() {
        owner.setPlaying(false, false);
        owner.getFlyByCamera().setDragToRotate(true);
        owner.getInputManager().setCursorVisible(true);
        nifty.gotoScreen("gameover");
    }
    
    /**
     *
     * @param nextScreen
     */
    public void nextScreen(String nextScreen){
        nifty.gotoScreen(nextScreen);
    }
}
